package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.resp.UserInfoResp;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsCampusGuide;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsCampusGuideReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.SmsWnsCampusGuideResp;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsCampusGuideMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsCampusGuideService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 校园指南表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-28
 */
@Service
public class SmsWnsCampusGuideServiceImpl extends ServiceImpl<SmsWnsCampusGuideMapper, SmsWnsCampusGuide> implements SmsWnsCampusGuideService {

    @Resource
    private SmsWnsCampusGuideMapper smsWnsCampusGuideMapper;
    @Resource
    private SmsUserMapper smsUserMapper;

    @Override
    public ResponseResult addCampusGuide(String userId, SmsWnsCampusGuideReq addReq) {
        if (isNullOrEmpty(addReq.getGuideTheme(),addReq.getGuideContent(),addReq.getIsUse())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsWnsCampusGuide smsWnsCampusGuide = new SmsWnsCampusGuide();
        BeanUtils.copyProperties(addReq,smsWnsCampusGuide);
        smsWnsCampusGuide.setCreateDate(new Date());
        smsWnsCampusGuide.setUserId(userId);
        smsWnsCampusGuideMapper.insert(smsWnsCampusGuide);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedCampusGuide(SmsWnsCampusGuideReq deletedReq) {
        if (isNullOrEmpty(deletedReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsWnsCampusGuide smsWnsCampusGuide = smsWnsCampusGuideMapper.selectById(deletedReq.getId());
        if (isNullOrEmpty(smsWnsCampusGuide)) {
            return CommonResult.failed(CommonCodeEnum.CAMPUS_GUIDE_NOT_EXIST);
        }
        smsWnsCampusGuideMapper.deleteById(deletedReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateCampusGuide(SmsWnsCampusGuideReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(),updateReq.getGuideTheme(),updateReq.getGuideContent(),updateReq.getIsUse())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsWnsCampusGuide smsWnsCampusGuide = smsWnsCampusGuideMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(smsWnsCampusGuide)) {
            return CommonResult.failed(CommonCodeEnum.CAMPUS_GUIDE_NOT_EXIST);
        }
        BeanUtils.copyProperties(updateReq,smsWnsCampusGuide);
        smsWnsCampusGuideMapper.updateById(smsWnsCampusGuide);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getCampusGuideDetail(String id) {
        if (isNullOrEmpty(id)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsWnsCampusGuide smsWnsCampusGuide = smsWnsCampusGuideMapper.selectById(id);
        if (isNullOrEmpty(smsWnsCampusGuide)) {
            return CommonResult.failed(CommonCodeEnum.CAMPUS_GUIDE_NOT_EXIST);
        }
        SmsWnsCampusGuideResp smsWnsCampusGuideResp = new SmsWnsCampusGuideResp();
        UserInfoResp userInfoResp = new UserInfoResp();
        BeanUtils.copyProperties(smsWnsCampusGuide,smsWnsCampusGuideResp);
        SmsUser smsUser = smsUserMapper.selectById(smsWnsCampusGuide.getUserId());
        if (!isNullOrEmpty(smsUser)) {
            BeanUtils.copyProperties(smsUser,userInfoResp);
        }
        smsWnsCampusGuideResp.setUserInfo(userInfoResp);
        return CommonResult.success(smsWnsCampusGuideResp);
    }

    @Override
    public ResponseResult getCampusGuideList(String createDate, String guideTheme, String publisher, Integer isUse, Integer pageNum, Integer pageSize) {
        Page<SmsWnsCampusGuide> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsWnsCampusGuide> campusGuideLambdaQueryWrapper = new LambdaQueryWrapper<SmsWnsCampusGuide>()
                .orderByDesc(SmsWnsCampusGuide::getCreateDate);
        if (!isNullOrEmpty(createDate)) {
            campusGuideLambdaQueryWrapper.eq(SmsWnsCampusGuide::getCreateDate,createDate);
        }
        if (!isNullOrEmpty(guideTheme)) {
            campusGuideLambdaQueryWrapper.eq(SmsWnsCampusGuide::getGuideTheme,guideTheme);
        }
        if (!isNullOrEmpty(publisher)) {
            List<String> userIdList = smsUserMapper.selectList(new LambdaQueryWrapper<SmsUser>()
                    .like(SmsUser::getUsername, publisher))
                    .stream().map(SmsUser::getId).collect(Collectors.toList());
            campusGuideLambdaQueryWrapper.in(SmsWnsCampusGuide::getUserId,userIdList.isEmpty() ? "" : userIdList);
        }
        if (!isNullOrEmpty(isUse)) {
            campusGuideLambdaQueryWrapper.eq(SmsWnsCampusGuide::getIsUse,isUse);
        }
        smsWnsCampusGuideMapper.selectPage(page,campusGuideLambdaQueryWrapper);
        List<SmsWnsCampusGuideResp> campusGuideList = page.getRecords()
                .stream()
                .map(smsWnsCampusGuide -> {
                    SmsWnsCampusGuideResp smsWnsCampusGuideResp = new SmsWnsCampusGuideResp();
                    UserInfoResp userInfoResp = new UserInfoResp();
                    BeanUtils.copyProperties(smsWnsCampusGuide,smsWnsCampusGuideResp);
                    SmsUser smsUser = smsUserMapper.selectById(smsWnsCampusGuide.getUserId());
                    if (!isNullOrEmpty(smsUser)) {
                        BeanUtils.copyProperties(smsUser,userInfoResp);
                    }
                    smsWnsCampusGuideResp.setUserInfo(userInfoResp);
                    return smsWnsCampusGuideResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(campusGuideList,(int)page.getTotal());
    }
}
